//
//  AppSettingViewController.m
//  location
//
//  Created by Dongxiang Xu on 2025/6/9.
//

#import "AppSettingViewController.h"
#import "AboutViewController.h"
#import "SettingViewController.h"
#import "MapSettingViewController.h"
#import "ContactUsViewController.h"
#import "LoginViewController.h"
#import "XDXLocationManager.h"
#import "LoginoutView.h"

@interface AppSettingViewController ()

@end

@implementation AppSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = LightBGColor;
    [self initView];
}

- (void)initView
{
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(16, NAVHEIGHT+6, SCREEN_WIDTH-32, 32)];
    title.font = REAL_MEDIUM_FONT(24);
    title.text = @"设置";
    title.textColor = RealBlackColor;
    [self.view addSubview:title];
    
    UIButton *map = [[UIButton alloc] initWithFrame:CGRectMake(16, title.maxY+16, SCREEN_WIDTH-32, 48)];
    map.backgroundColor = RealWhiteColor;
    map.layer.masksToBounds = YES;
    [map.layer setCornerRadius:16];
    [self.view addSubview:map];
    [[map rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        MapSettingViewController *vc = [[MapSettingViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }];
    
    UIImageView *mapImg = [[UIImageView alloc] initWithFrame:CGRectMake(16, (map.height-24)/2, 24, 24)];
    mapImg.image = [UIImage imageNamed:@"map_type"];
    [map addSubview:mapImg];
    
    UILabel *mapLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(mapImg.frame)+6, 0, 104, map.height)];
    mapLabel.text = @"地图类型";
    mapLabel.textColor = GraryHeavyLightColor;
    mapLabel.font = MEDIUM_FONT(15);
    [map addSubview:mapLabel];
    
    NSInteger mapType = [[[NSUserDefaults standardUserDefaults] objectForKey:MapType] integerValue];
    UILabel *mapSub = [[UILabel alloc] initWithFrame:CGRectMake(map.width-68, 0, 34, map.height)];
    if (mapType==0) {
        mapSub.text = @"街道";
    }else if (mapType==1){
        mapSub.text = @"卫星";
    }
    mapSub.font = DEFAULT_FONT(14);
    mapSub.textAlignment = NSTextAlignmentRight;
    mapSub.textColor = GraryTooHeavyColor;
    [map addSubview:mapSub];
    
    UIImageView *mapArrow = [[UIImageView alloc] initWithFrame:CGRectMake(map.width-30, (map.height-12)/2, 12, 12)];
    mapArrow.image = [UIImage imageNamed:@"arrow_right"];
    [map addSubview:mapArrow];
    
    NSString *appDisplayName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"];
    if (appDisplayName == nil) {
        appDisplayName = [[NSBundle mainBundle] bundleIdentifier];
    }
    NSArray *infoArray = @[@{@"img":@"dev_talk",@"title":@"和开发者聊聊"},@{@"img":@"about",@"title":[NSString stringWithFormat:@"关于%@",appDisplayName]}];
    UIView *infoView = [[UIView alloc] initWithFrame:CGRectMake(16, CGRectGetMaxY(map.frame)+16, SCREEN_WIDTH-32, 48*infoArray.count)];
    infoView.backgroundColor = RealWhiteColor;
    infoView.layer.masksToBounds = YES;
    [infoView.layer setCornerRadius:16];
    [self.view addSubview:infoView];
    for (int i = 0; i < infoArray.count; i ++) {
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, i*48, infoView.width, 48)];
        button.backgroundColor = RealWhiteColor;
        button.layer.masksToBounds = YES;
        [infoView addSubview:button];
        
        UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(16, (button.height-24)/2, 24, 24)];
        img.image = [UIImage imageNamed:infoArray[i][@"img"]];
        [button addSubview:img];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(img.frame)+6, 0, button.width-CGRectGetMaxX(img.frame)-66, button.height)];
        label.text = infoArray[i][@"title"];
        label.textColor = GraryHeavyLightColor;
        label.font = MEDIUM_FONT(15);
        [button addSubview:label];
        
        UIImageView *arrow = [[UIImageView alloc] initWithFrame:CGRectMake(button.width-30, (button.height-12)/2, 12, 12)];
        arrow.image = [UIImage imageNamed:@"arrow_right"];
        [button addSubview:arrow];
        
        if (i<(infoArray.count-1)) {
            UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(16, button.height-1, button.width-32, 1)];
            line.image = [UIImage imageNamed:@"line"];
            [button addSubview:line];
        }
        
        [[button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            if (i==0) {
                ContactUsViewController *vc = [[ContactUsViewController alloc] init];
                [self.navigationController pushViewController:vc animated:YES];
            }else if (i==1){
                AboutViewController *vc = [[AboutViewController alloc] init];
                [self.navigationController pushViewController:vc animated:YES];
            }
        }];
    }
    
    NSArray *settingArray = @[@{@"img":@"account_safe",@"title":@"账号与安全"},@{@"img":@"logout",@"title":@"退出登录"}];
    UIView *settingView = [[UIView alloc] initWithFrame:CGRectMake(16, CGRectGetMaxY(infoView.frame)+16, SCREEN_WIDTH-32, 48*settingArray.count)];
    settingView.backgroundColor = RealWhiteColor;
    settingView.layer.masksToBounds = YES;
    [settingView.layer setCornerRadius:16];
    [self.view addSubview:settingView];
    for (int i = 0; i < settingArray.count; i ++) {
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, i*48, settingView.width, 48)];
        button.backgroundColor = RealWhiteColor;
        button.layer.masksToBounds = YES;
        [settingView addSubview:button];
        
        UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(16, (button.height-24)/2, 24, 24)];
        img.image = [UIImage imageNamed:settingArray[i][@"img"]];
        [button addSubview:img];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(img.frame)+6, 0, 104, button.height)];
        label.text = settingArray[i][@"title"];
        label.textColor = GraryHeavyLightColor;
        label.font = MEDIUM_FONT(15);
        [button addSubview:label];
        
        if (i==0) {
            UIImageView *arrow = [[UIImageView alloc] initWithFrame:CGRectMake(button.width-30, (button.height-12)/2, 12, 12)];
            arrow.image = [UIImage imageNamed:@"arrow_right"];
            [button addSubview:arrow];
            
            UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(16, button.height-1, button.width-32, 1)];
            line.image = [UIImage imageNamed:@"line"];
            [button addSubview:line];
        }
        [[button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            if (i==0) {
                SettingViewController *vc = [[SettingViewController alloc] init];
                [self.navigationController pushViewController:vc animated:YES];
            }else if (i==1){
                LoginoutView *view = [[LoginoutView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
                [self.view addSubview:view];
                view.LoginoutBlock = ^{
                    [[XDXSocket shareInstance] close];
                    
                    [XDXShareInstance shareInstance].userId = @"";
                    [XDXShareInstance shareInstance].nickName = @"";
                    [XDXShareInstance shareInstance].access_token = @"";
                    [XDXShareInstance shareInstance].xg_token = @"";
                                        
                    [[NSUserDefaults standardUserDefaults] setValue:@"" forKey:UserId];
                    [[NSUserDefaults standardUserDefaults] synchronize];
                    [[NSUserDefaults standardUserDefaults] setValue:@"" forKey:AccessToken];
                    [[NSUserDefaults standardUserDefaults] synchronize];
                    [[NSUserDefaults standardUserDefaults] setValue:@"" forKey:NickName];
                    [[NSUserDefaults standardUserDefaults] synchronize];
                    [[NSUserDefaults standardUserDefaults] setValue:@"" forKey:Avatar];
                    [[NSUserDefaults standardUserDefaults] synchronize];
                    [[NSUserDefaults standardUserDefaults] setValue:@"" forKey:ShowTraffic];
                    [[NSUserDefaults standardUserDefaults] synchronize];
                    [[NSUserDefaults standardUserDefaults] setValue:@"" forKey:RoadNet];
                    [[NSUserDefaults standardUserDefaults] synchronize];
                    [[NSUserDefaults standardUserDefaults] setValue:@"" forKey:MapType];
                    [[NSUserDefaults standardUserDefaults] synchronize];
                    [[NSUserDefaults standardUserDefaults] setValue:@"" forKey:GroupId];
                    [[NSUserDefaults standardUserDefaults] synchronize];
                    [[NSUserDefaults standardUserDefaults] setValue:@"" forKey:GroupListTime];
                    [[NSUserDefaults standardUserDefaults] synchronize];
                    [[NSUserDefaults standardUserDefaults] setValue:@"" forKey:LastRegionSendTime];
                    [[NSUserDefaults standardUserDefaults] synchronize];
                    
                    [[XDXLocationManager shareInstance].locationManager stopUpdatingHeading];
                    [[XDXLocationManager shareInstance].locationManager stopUpdatingLocation];
                    [[XDXLocationManager shareInstance].locationManager stopMonitoringSignificantLocationChanges];
                    [[XDXLocationManager shareInstance].locationManager stopMonitoringVisits];
                    
                    {
                        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
                        NSString *documentsDirectory = [paths objectAtIndex:0];
                        NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"GroupList.plist"];
                        // 删除plist文件
                        NSFileManager *fileManager = [NSFileManager defaultManager];
                        if ([fileManager fileExistsAtPath:plistPath]) {
                            [fileManager removeItemAtPath:plistPath error:nil];
                        }
                    }
                    {
                        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
                        NSString *documentsDirectory = [paths objectAtIndex:0];
                        NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"Regions.plist"];
                        // 删除plist文件
                        NSFileManager *fileManager = [NSFileManager defaultManager];
                        if ([fileManager fileExistsAtPath:plistPath]) {
                            [fileManager removeItemAtPath:plistPath error:nil];
                        }
                    }
                    {
                        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
                        NSString *documentsDirectory = [paths objectAtIndex:0];
                        NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"driveLog.plist"];
                        // 删除plist文件
                        NSFileManager *fileManager = [NSFileManager defaultManager];
                        if ([fileManager fileExistsAtPath:plistPath]) {
                            [fileManager removeItemAtPath:plistPath error:nil];
                        }
                    }
                    {
                        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
                        NSString *documentsDirectory = [paths objectAtIndex:0];
                        NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"regionLog.plist"];
                        // 删除plist文件
                        NSFileManager *fileManager = [NSFileManager defaultManager];
                        if ([fileManager fileExistsAtPath:plistPath]) {
                            [fileManager removeItemAtPath:plistPath error:nil];
                        }
                    }
                    
                    LoginViewController *vc = [[LoginViewController alloc] init];
                    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
                    [UIApplication sharedApplication].keyWindow.rootViewController = nav;
                    [[UIApplication sharedApplication].keyWindow makeKeyAndVisible];
                };
            }
        }];
    }
    
    UILabel *version = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(settingView.frame)+30, SCREEN_WIDTH, 17)];
    version.textAlignment = NSTextAlignmentCenter;
    version.font = DEFAULT_FONT(12);
    version.textColor = GraryTooHeavyColor;
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    version.text = [NSString stringWithFormat:@"Version %@ (Build %@)",[infoDictionary objectForKey:@"CFBundleShortVersionString"],[infoDictionary objectForKey:(NSString *)kCFBundleVersionKey]];
    [self.view addSubview:version];
}

@end
